import Vue from 'vue'
import Router from 'vue-router';

const originalPush = Router.prototype.push;
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err);
};

Vue.use(Router);
let router = new Router({
  routes: [
    {
      path: "/login",
      name: "login",
      title: "登录",
      component: () => import('./views/login/index.vue'),
      meta: {
        title: ['登录', '登录']
      }
    },
    {
      path: "/my-blog",
      name: "my-blog",
      title: "我的博客",
      component: () => import('./views/myBlog/list.vue'),
      meta: {
        title: ['博客管理', '我的博客']
      }
    },
    {
      path: "/blog-edit",
      name: "blog-edit",
      title: "博客编辑",
      component: () => import('./views/myBlog/edit.vue'),
      meta: {
        title: ['博客管理', '博客编辑']
      }
    },
    {
      path: "/my-project",
      name: "my-project",
      title: "我的项目",
      component: () => import('./views/myProject/list.vue'),
      meta: {
        title: ['项目管理', '我的项目']
      }
    },
    {
      path: "/project-edit",
      name: "project-edit",
      title: "项目编辑",
      component: () => import('./views/myProject/edit.vue'),
      meta: {
        title: ['项目管理', '项目编辑']
      }
    },
    {
      path: "/link-list",
      name: "link-list",
      title: "友情链接",
      component: () => import('./views/linkList/index.vue'),
      meta: {
        title: ['链接管理', '友情链接']
      }
    },
    {
      path: "/user-list",
      name: "user-list",
      title: "用户列表",
      component: () => import('./views/user/index.vue'),
      meta: {
        title: ['用户管理', '用户列表']
      }
    },
    {
      path: "/discuss-list",
      name: "discuss-list",
      title: "留言列表",
      component: () => import('./views/discussList/index.vue'),
      meta: {
        title: ['留言管理', '留言列表']
      }
    },
    {
      path: "/test",
      name: "test",
      title: "测试",
      component: () => import('./views/test.vue'),
      meta: {
        title: ['测试', '测试']
      }
    },
  ]
})
router.beforeEach((to, from, next) => {
  if (!sessionStorage.username) {
    if (to.name === 'login') {
      next();
    } else {
      next({ name: 'login' });
    }
  } else {
    next();
  }
});

router.afterEach(to => {
  if (to && to.meta && to.meta.title) {
    document.title = to.meta.title[1];
  }
});
export default router;
